<?php

namespace app\common;
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;

class Qiniu {
    // 需要填写你的 Access Key 和 Secret Key
       public $accessKey;
       public $secretKey;
       public $bucket;
       public function __construct()
       {
           $accessKey = 'sGJG6gScXhjlaG1ZlgX3Q4Of0DtBk70EIVCIRc5k';
           $secretKey = 'VHhTATJMeJM8F7GRX8lqnPcOvkmm_4PTAGbOFYNR';
           $bucket = 'shiyuan-photoshare';

           $this->accessKey = $accessKey;
           $this->secretKey = $secretKey;
           $this->bucket = $bucket;
       }

    public function uploadFile($key,$filePath){

// 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);
// 生成上传 Token
        $expires = 7200;
        $policy = null;
        $token = $auth->uploadToken($this->bucket, null, $expires, $policy, true);
// 要上传文件的本地路径
//        $filePath = '../public/resource/apk/shiyuan.apk';
// 上传到七牛后保存的文件名
//        $key = '01.apk';
// 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
//        echo "\n====> putFile result: \n";
        if ($err !== null) {
//            var_dump($err);
//            return $err;
            return "0";
        } else {
//            var_dump($ret);
//            return $ret;
            return "1";
        }
    }

    public function deleteFile($key){
        $auth = new Auth($this->accessKey, $this->secretKey);
        $config = new \Qiniu\Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
        $err = $bucketManager->delete($this->bucket, $key);
        if ($err) {
            print_r($err);
        }
    }

}